//
//  HELBaseViewVC.m
//  HandheldELearn
//
//  Created by wsj2015 on 16/7/11.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELBaseViewVC.h"


#import "Masonry.h"
#import "WSJLoadingView.h"
#import "WSJLoadFailedView.h"
#import "UIViewController+BackButtonHandler.h"


@interface HELBaseViewVC ()



@property(nonatomic, strong)UILabel *noConnectionLabel;
@property(nonatomic, strong)UIView *bannerView;





@end

@implementation HELBaseViewVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.isShowNetwork = NO;
        
        
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
//    self.navigationController.navigationBar.backItem.hidesBackButton = NO;

    [self configUI];
//    self.mode = MMReachabilityModeResize;
    if (self.isShowNetwork) {
       
        [[HELNetworkManager sharedMg] addObserver:self forKeyPath:@"netStatus" options:NSKeyValueObservingOptionNew context:nil];
    }
    
}




- (void)viewWillAppear:(BOOL)animated{
   
    [super viewWillAppear:animated];
    if (self.isShowNetwork) {
        if (![HELNetworkManager sharedMg].netStatus) {
            [self.noNetView appear];
        }else{
            [self.noNetView disappear];
        }
    }
    
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
   
   
    
    if ([keyPath isEqualToString:@"netStatus"]) {
        NSNumber *value = change[@"new"];
        if (value.integerValue == 0) {
            [self.noNetView appear];
        }else{
            [self.noNetView disappear];
        }
    }

}

- (void)dealloc{
    if (self.isShowNetwork) {
            [[HELNetworkManager sharedMg] removeObserver:self forKeyPath:@"netStatus"];
    }
    

  
}
- (void )configUI{

}

- (BOOL)navigationShouldPopOnBackButton{
    
    
    [self wsj_hideLodingGif];
   
    return YES;
}

- (WLReloadPromptView *)noNetView{
    
    
    //    @property (strong, nonatomic) IBInspectable UIColor *ringColor;         /*< 圆圈的颜色 */
    //    @property (strong, nonatomic) IBInspectable UIColor *WIFIColor;         /*< WIFI Logo 的颜色 */
    //    @property (strong, nonatomic) IBInspectable UIColor *promptTextColor;   /*< 提示文字的颜色 */
    //    @property (strong, nonatomic) IBInspectable UIColor *reloadTitleColor;  /*< 按钮标题的颜色 */
    //    @property (strong, nonatomic) IBInspectable UIColor *borderColor;       /*< 按钮边框的颜色 */
    //    @property (strong, nonatomic) IBInspectable UIColor *borderFillColor;   /*< 按钮背景色 */
    //    @property (copy, nonatomic) IBInspectable NSString *promptText;         /*< 提示文字 */
    //    @property (copy, nonatomic) IBInspectable NSString *reloadTitle;        /*< 按钮标题 */
    //    @property (assign, nonatomic) IBInspectable CGFloat borderRadius;       /*< 按钮边框圆角 */
    //
    //    @property (nullable, copy, nonatomic) void (^reloadActions)(void);      /*< 按钮点击的闭包 */
    if (!_noNetView) {
        _noNetView  =[[WLReloadPromptView alloc] initWithCoveredView:self.view];
        _noNetView.ringColor =kMainColor;
        _noNetView.reloadTitleColor =[UIColor whiteColor];
        _noNetView.borderColor =[UIColor clearColor];
        _noNetView.borderFillColor = kPinchColor;
        _noNetView.borderRadius = 10;
        [self.view addSubview:_noNetView];
    }
    return _noNetView;
    
}
//标题
- (void)titleView:(NSString *)title {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.center = CGPointMake(self.view.center.x, 42);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = title;
    [self.navigationItem setTitleView:titleLabel];
    
}


- (void)isShowBackBtn:(BOOL)isShow{
    
    UIBarButtonItem *item =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction:)];
    if (isShow) {
        self.navigationItem.leftBarButtonItem =item;

    }
    
}

- (void)backAction:(UIButton *)sender{
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (UIView*)bannerView {
    
    if (!_bannerView) {
        
        UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, 0.0, self.view.frame.size.width, 38.0)];
        [bannerView setBackgroundColor:[UIColor redColor]];
        
        UILabel *noConnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, 0.0, bannerView.frame.size.width, bannerView.frame.size.height)];
        [noConnectionLabel setFont:[UIFont boldSystemFontOfSize:16]];
        [noConnectionLabel setTextColor:[UIColor whiteColor]];
        [noConnectionLabel setShadowColor:[UIColor blackColor]];
        [noConnectionLabel setText:@"无法连接网络!"];
        [noConnectionLabel setTextAlignment:NSTextAlignmentCenter];
        [noConnectionLabel setBaselineAdjustment:UIBaselineAdjustmentAlignCenters];
        [noConnectionLabel setBackgroundColor:[UIColor clearColor]];
        [noConnectionLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = noConnectionLabel.bounds;
        gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.8 green:0 blue:0 alpha:0.8] CGColor],
                           (id)[[UIColor colorWithRed:0.4 green:0 blue:0 alpha:0.8] CGColor],
                           nil];
        [bannerView.layer insertSublayer:gradient atIndex:0];
        [bannerView addSubview:noConnectionLabel];
        [self.view.superview addSubview:bannerView];
        [self.view.superview sendSubviewToBack:bannerView];
        _bannerView = bannerView;
       
    }
    
    return _bannerView;
}











@end
